package org.wu.framework.authorization.platform.application.impl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForThaiBuddhistDate;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.wu.framework.authorization.annotation.RequiredAuthorization;
import org.wu.framework.authorization.platform.domain.model.sys.interface_.SysInterface;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.authorization.platform.application.SysPermissionApplication;
import org.springframework.web.bind.annotation.*;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.wu.framework.authorization.platform.domain.model.sys.permission.SysPermission;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionRemoveCommand;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionStoryCommand;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionUpdateCommand;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionQueryListCommand;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionQueryOneCommand;
import org.wu.framework.authorization.platform.application.assembler.SysPermissionDTOAssembler;
import org.wu.framework.authorization.platform.application.dto.SysPermissionDTO;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import jakarta.annotation.Resource;
import org.wu.framework.authorization.platform.domain.model.sys.permission.SysPermissionRepository;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionExcelExport;
import org.wu.framework.authorization.platform.application.command.sys.permission.SysPermissionExcelImport;

/**
 * describe 系统权限
 *
 * @author Jia wei Wu
 * @date 2025/08/03 01:55 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl
 **/
@LazyApplication
public class SysPermissionApplicationImpl implements SysPermissionApplication {

    @Resource
    SysPermissionRepository sysPermissionRepository;



    /**
     * describe 新增系统权限
     *
     * @param sysPermissionStoryCommand 新增系统权限
     * @return {@link Result<SysPermission>} 系统权限新增后领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysPermission> story(SysPermissionStoryCommand sysPermissionStoryCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionStoryCommand);
        return sysPermissionRepository.story(sysPermission);
    }

    /**
     * describe 批量新增系统权限
     *
     * @param sysPermissionStoryCommandList 批量新增系统权限
     * @return {@link Result<List<SysPermission>>} 系统权限新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysPermission>> batchStory(List<SysPermissionStoryCommand> sysPermissionStoryCommandList) {
        List<SysPermission> sysPermissionList = sysPermissionStoryCommandList.stream().map(SysPermissionDTOAssembler.INSTANCE::toSysPermission).collect(Collectors.toList());
        return sysPermissionRepository.batchStory(sysPermissionList);
    }

    /**
     * describe 更新系统权限
     *
     * @param sysPermissionUpdateCommand 更新系统权限
     * @return {@link Result<SysPermission>} 系统权限领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysPermission> updateOne(SysPermissionUpdateCommand sysPermissionUpdateCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionUpdateCommand);
        return sysPermissionRepository.story(sysPermission);
    }

    /**
     * describe 查询单个系统权限
     *
     * @param sysPermissionQueryOneCommand 查询单个系统权限
     * @return {@link Result<SysPermissionDTO>} 系统权限DTO对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysPermissionDTO> findOne(SysPermissionQueryOneCommand sysPermissionQueryOneCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionQueryOneCommand);
        return sysPermissionRepository.findOne(sysPermission).convert(SysPermissionDTOAssembler.INSTANCE::fromSysPermission);
    }

    /**
     * describe 查询多个系统权限
     *
     * @param sysPermissionQueryListCommand 查询多个系统权限
     * @return {@link Result<List<SysPermissionDTO>>} 系统权限DTO对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysPermissionDTO>> findList(SysPermissionQueryListCommand sysPermissionQueryListCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionQueryListCommand);
        return sysPermissionRepository.findList(sysPermission).convert(sysPermissions -> sysPermissions.stream().map(SysPermissionDTOAssembler.INSTANCE::fromSysPermission).collect(Collectors.toList()));
    }

    /**
     * 扫描系统权限
     *
     * @return
     */
    @Override
    public Result<List<SysPermissionDTO>> findScanPermission() {
        return sysPermissionRepository.findScanPermission();
    }

    /**
     * describe 分页查询多个系统权限
     *
     * @param sysPermissionQueryListCommand 分页查询多个系统权限
     * @return {@link Result<LazyPage<SysPermissionDTO>>} 分页系统权限DTO对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<LazyPage<SysPermissionDTO>> findPage(int size, int current, SysPermissionQueryListCommand sysPermissionQueryListCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionQueryListCommand);
        return sysPermissionRepository.findPage(size, current, sysPermission).convert(page -> page.convert(SysPermissionDTOAssembler.INSTANCE::fromSysPermission));
    }

    /**
     * describe 删除系统权限
     *
     * @param sysPermissionRemoveCommand 删除系统权限
     * @return {@link Result<SysPermission>} 系统权限
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysPermission> remove(SysPermissionRemoveCommand sysPermissionRemoveCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionRemoveCommand);
        return sysPermissionRepository.remove(sysPermission);
    }

    /**
     * describe 导出系统权限
     *
     * @param sysPermissionQueryListCommand 导出系统权限
     * @return {@link List<SysPermissionExcelExport>} 系统权限
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public List<SysPermissionExcelExport> exportExcel(SysPermissionQueryListCommand sysPermissionQueryListCommand) {
        SysPermission sysPermission = SysPermissionDTOAssembler.INSTANCE.toSysPermission(sysPermissionQueryListCommand);
        return sysPermissionRepository.findList(sysPermission).applyOther(sysPermissions -> sysPermissions.stream().map(SysPermissionDTOAssembler.INSTANCE::fromSysPermissionToExcelExport).collect(Collectors.toList()));
    }

    /**
     * describe 导入系统权限
     *
     * @param sysPermissionExcelImportList 导入系统权限
     * @return {@link Result<SysPermissionExcelImport>} 系统权限
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysPermissionExcelImport>> importExcel(List<SysPermissionExcelImport> sysPermissionExcelImportList) {
        List<SysPermission> sysPermissionList = sysPermissionExcelImportList.stream().map(SysPermissionDTOAssembler.INSTANCE::toSysPermission).collect(Collectors.toList());
        sysPermissionRepository.batchStory(sysPermissionList);
        return ResultFactory.successOf(sysPermissionExcelImportList);
    }
}